<?php
include_once F_SOURCE . 'vbc/vbc.ctrl.class.php';
include_once F_SOURCE . 'launch/launch.model.class.php';
include_once F_SOURCE . 'ads/ads.model.class.php';

/**
 * 批量导入创意管理CTRL类
 * 
 * @author Dean
 *
 */
class importCtrl extends Ctrl{
	
	public function show() {
		$view = new View();
		
		$data = array(
			'op_select' => array('import'=>"class=select"),
		);
		
		$view->display($data, 'import_show.html');
	}

	public function import(){
		
		$view = new View();
		
		$url = page_save($_FILES['page'],F_UPLOAD);
		$urlArray = explode('@@',$url);
		
		//如果出现错误，进行错误提示
		if($urlArray[0]=='true'){
			$fileName = trim(substr(strrchr($urlArray[1], '/'), 1));
			$url = $urlArray[1];
			
			$this->showMessage('?do=import.waiting&url='.$fileName, 'upload_success');
		}else{
			$this->showMessage('?do=import.show', $urlArray[1]);
		}
		
		exit;
	}
	
	public function importads(){
		$url = getGetStringParam('url');
		$fileName = checkFilename($url, F_UPLOAD);
		if($fileName===false){
			go('index.php?do=main.show&msg=error');
		}
		
		$this->addads($fileName);
		go('index.php?do=main.show&msg=success_ads');
		exit;
	}
	
	public function importlaunch(){
		$url = getGetStringParam('url');
		$fileName = checkFilename($url, F_UPLOAD);
		if($fileName===false){
			go('index.php?do=main.show&msg=error');
		}
		
		$this->addlaunch($fileName);
		go('index.php?do=main.show&msg=success_launch');
		exit;
	}

	public function waiting(){
		$view = new View();
		$view->display(array(), 'import_waiting.html');
	}
	
	public function addads($url){
		$handle = fopen(F_UPLOAD.$url, "r");
		if($handle===false)return false;
		$key = 0;
		while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
			$c = count($data);
			for ($x=0;$x<$c;$x++) $arrCSV[$key][$x] = $data[$x];
			$key++;
		}
		fclose($handle);
	
		$vbcCtrl = new vbcCtrl();
		
		$vbcNum = 0;
		$kwsNum = 0;
		$jsonIndex = 0;
		$idArr = array();
		foreach ($arrCSV as $csv_key => $csv_value) {
			if(intval($csv_value[0])<=0 || count($csv_value)!=10)continue;
			$temp['id'] = "'".addslashes($csv_value[0])."'";
			$temp['name'] = "'".addslashes(formatToUTF8($csv_value[1]))."'";
			$temp['launch_name'] = "'".addslashes(formatToUTF8($csv_value[2]))."'";
			$temp['start'] = "'".addslashes(formatCsvDate($csv_value[3]))."'";
			$temp['end'] = "'".addslashes(formatCsvDate($csv_value[4]))."'";
			$temp['weight'] = "'".addslashes($csv_value[5])."'";
			$temp['clickurl'] = "'".addslashes($csv_value[6])."'";
			$temp['size'] = "'".addslashes(formatToUTF8($csv_value[7]))."'";
			$temp['window'] = "'".addslashes(formatToUTF8($csv_value[8]))."'";
			$temp['status'] = "'".addslashes(formatToUTF8($csv_value[9]))."'";
			$temp['op'] = "'o'";
			$type = getAdsTypeByName($temp['launch_name']);
			if($type=='vbc'){
				$checkName=$vbcCtrl->insertGroupByImport($csv_value[1], $csv_value[0]);
				if($checkName===false)continue;
				$vbcNum++;
			}else{
				$kwsNum++;
			}
			$temp['type'] = "'".$type."'";
			$temp['createTime'] = "'".getNowTime()."'";
			$temp['modifyTime'] = "'".getNowTime()."'";
	
			$idArr[$jsonIndex] = $csv_value[0];
			$strArr[$jsonIndex] = '('.implode(',', $temp).')';
			$jsonIndex++;
		}
	
		$fields = array(
				'`id`',
				'`name`',
				'`launch_name`',
				'`start`',
				'`end`',
				'`weight`',
				'`click_url`',
				'`size`',
				'`target_window`',
				'`status`',
				'`op`',
				'`type`',
				'`createTime`',
				'`modifyTime`'
		);
	
		setcookie("Ads_kws", $kwsNum, time()+259200);  //set cookie
		setcookie("Ads_vbc", $vbcNum, time()+259200);  //set cookie
		
		if(count($strArr)>0){
// 			$adsModel = new adsModel();
// 			$returnID = $adsModel->importDataToDB($fields, $strArr);
			$existArr = $this->checkKWLaunchExist($fields, $strArr, $idArr);
			if(count($existArr)>0){
				setcookie("Ads_exist_kws", implode(',', $existArr), time()+259200);  //set cookie
			}
		}
	
		return true;
// 		if($returnID > 0){
// 			return true;
// 		}else{
// 			return false;
// 		}
	}
	
	public function addlaunch($url){
	
// 		$url = getPostStringParam('url');
	
		$handle = fopen(F_UPLOAD.$url, "r");
		if($handle===false)return false;
		$key = 0;
		while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
			$c = count($data);
			for ($x=0;$x<$c;$x++) $arrCSV[$key][$x] = $data[$x];
			$key++;
		}
		fclose($handle);
	
		$vbcNum = 0;
		$kwsNum = 0;
		$jsonIndex = 0;
		foreach ($arrCSV as $csv_key => $csv_value) {
			if(intval($csv_value[0])<=0 || count($csv_value)!=14)continue;
			$temp['id'] = "'".addslashes($csv_value[0])."'";
			$temp['name'] = "'".addslashes(formatToUTF8($csv_value[1]))."'";
			$temp['order'] = "'".addslashes(formatToUTF8($csv_value[2]))."'";
			$temp['activity'] = "'".addslashes(formatToUTF8($csv_value[3]))."'";
			$temp['adsnum'] = "'".addslashes($csv_value[4])."'";
			$temp['start'] = "'".addslashes(formatCsvDate($csv_value[5]))."'";
			$temp['end'] = "'".addslashes(formatCsvDate($csv_value[6]))."'";
			$temp['priority'] = "'".addslashes($csv_value[7])."'";
			$temp['daynumber'] = "'".addslashes($csv_value[8])."'";
// 			$temp['type'] = "'".addslashes(formatToUTF8($csv_value[9]))."'";
			$type = getAdsTypeByName($temp['name']);
			if($type=='vbc'){
				
				
				
				$vbcNum++;
			}else{
				$kwsNum++;
			}
			$temp['type'] = "'".getAdsTypeByName($temp['name'])."'";
			$temp['kind'] = "'".addslashes(formatToUTF8($csv_value[10]))."'";
			$temp['orientation'] = "'".addslashes(formatToUTF8($csv_value[11]))."'";
			$temp['size'] = "'".addslashes(formatToUTF8($csv_value[12]))."'";
			$temp['status'] = "'".addslashes(formatToUTF8($csv_value[13]))."'";
			$temp['op'] = "'o'";
			$temp['createTime'] = "'".getNowTime()."'";
	
			$strArr[$jsonIndex] = '('.implode(',', $temp).')';
			$jsonIndex++;
		}
	
		$fields = array(
				'`id`',
				'`name`',
				'`order`',
				'`activity`',
				'`adsnum`',
				'`start`',
				'`end`',
				'`priority`',
				'`daynumber`',
				'`type`',
				'`kind`',
				'`orientation`',
				'`size`',
				'`status`',
				'`op`',
				'`createTime`',
		);
	
		if(count($strArr)>0){
			$launchModel = new launchModel();
			$returnID = $launchModel->importDataToDB($fields, $strArr);
		}
		setcookie("Launch_kws", $kwsNum, time()+259200);  //set cookie
		setcookie("Launch_vbc", $vbcNum, time()+259200);  //set cookie
		
		// set default 'order' and 'activity'
		$vbcCtrl = new vbcCtrl();
		$vbcCtrl->inputOrderintoGroup();
		
		if($returnID > 0){
			return true;
		}else{
			return false;
		}
	}
	
	private function checkKWLaunchExist($fields, $strArr, $idArr){
		$launchModel = new launchModel();
		$adsModel = new adsModel();
		$existArr = array();
		foreach ($strArr as $key => $value){
			$launchInfo = $launchModel->findOne($idArr[$key]);
			if(isset($launchInfo->id)){
				$existArr[] = $idArr[$key];
			}else{
				$adsModel->importDataToDB2($fields, $value);
			}
		}
		return $existArr;
	}
	
}


?>